package com.ui
{
	import com.engine.AwayEngine;
	import com.engine.SurfaceResize;
	import com.greensock.TweenLite;
	import com.manager.Vision;
	import com.utils.PowerLoader;
	import com.vo.PartVo;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.events.Touch;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.textures.Texture;
	
	/**
	 * 汽车信息详解
	 */	
	public class DetailFace extends SurfaceResize
	{
		private static var menu:DetailFace;
		public static function getInstance():DetailFace{
			if(menu== null) menu = new DetailFace();
			return menu;
		}
		
		public function show():void{
			initFace();
		}
		
		private var infoLayer:Sprite;
		private var partDes:TextField;
		private var partName:TextField;
		private var schedule:TextField;
		private var budget:TextField;
		private var bill:TextField;
		private var buy:TextField;
		private function initFace():void
		{
			if(infoLayer == null){
				infoLayer = new Sprite();
				
				createBack();
				
				createInfoText();
				
				crateArrowBtn();
				
				createCloseBtn();
				
				PowerLoader.loadFile("assets/Cartwheel-1.png",loadImage);
				
				infoLayer.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			}
			infoLayer.y = 70;
			Vision.addView(Vision.MAIN,infoLayer);
		}
		
		private function addToStage(e:Event):void
		{
			infoLayer.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			resize();
		}
		
		protected override function resize():void
		{
			infoLayer.x = AwayEngine.senceWidth - 276;
		}
		
		protected function createCloseBtn():void{
			var btnImage1:Image = CustomSkin.getImage("closeBtn");
			infoLayer.addChild(btnImage1);
			btnImage1.addEventListener(starling.events.TouchEvent.TOUCH,onCloseBtn)
		}
		
		private function onCloseBtn(e:starling.events.TouchEvent):void{
			var touch:Touch = e.getTouch(e.target as Image);
			var image:Image = e.target as Image;
			if(touch != null){
				if(touch.phase == TouchPhase.BEGAN){
					TweenLite.to(infoLayer,.2,{x:AwayEngine.senceWidth});
				}
			}
		}
		
		private function loadImage(bmp:Bitmap):void
		{
			var btnImage01:Image = Image.fromBitmap(bmp);
			btnImage01.x = 23;
			btnImage01.y = 75;
			infoLayer.addChild(btnImage01);
			var btnImage02:Image = Image.fromBitmap(bmp);
			btnImage02.x = 27 + btnImage01.width;
			btnImage02.y = 75;
			infoLayer.addChild(btnImage02);
			var btnImage03:Image = Image.fromBitmap(bmp);
			btnImage03.x = 31 + btnImage01.width*2;
			btnImage03.y = 75;
			infoLayer.addChild(btnImage03);
		}
		
		private function createBack():void{
			var bg1:Image = Vision.introduce(276,50,0,0,0xffffff);
			var bg2:Image = Vision.introduce(276,340,0,50,0xffffff);
			var bg3:Image = Vision.introduce(276,70,0,390,0xffffff);
			infoLayer.addChild(bg1);
			infoLayer.addChild(bg2);
			infoLayer.addChild(bg3);
			bg1.alpha = bg2.alpha = bg3.alpha = .2;
		}
		
		private function crateArrowBtn():void{
			var lastBtn:Image = CustomSkin.getImage("SmallArrows");
			lastBtn.x = 5;
			lastBtn.y = 95;
			infoLayer.addChild(lastBtn);
			var nextBtn:Image = CustomSkin.getImage("SmallArrows");
			nextBtn.x = 270;
			nextBtn.y = 95;
			nextBtn.scaleX = -1;
			infoLayer.addChild(nextBtn);
//			var lastBtn:Button = new Button(CustomSkin.getSkinTexture("SmallLeftArrows"),"",CustomSkin.getSkinTexture("SmallLeftArrowsHover"));
//			lastBtn.x = 5;
//			lastBtn.y = 95;
//			infoLayer.addChild(lastBtn);
//			var nextBtn:Button = new Button(CustomSkin.getSkinTexture("SmallRightArrows"),"",CustomSkin.getSkinTexture("SmallRightArrowsHover"));
//			nextBtn.x = 260;
//			nextBtn.y = 95;
//			nextBtn.scaleX = -1;
//			infoLayer.addChild(nextBtn);
		}
		
		private function createInfoText():void{
			partDes = Vision.createText('',0,25,195,230,211);
			partName = Vision.createText('',0,120,12,240,50);
			schedule = Vision.createText(" ￥ ",0x8b0000,65,404,230,50);
			budget = Vision.createText(" ￥ ",0x8b0000,65,430,240,50);
			bill = Vision.createText(" ￥ ",0x8b0000,195,404,240,50);
			buy = Vision.createText(" ￥ ",0x8b0000,195,430,240,50);
			var scheduleText:TextField = Vision.createText("预定: ",0,20,404,50,100);
			var budgetText:TextField = Vision.createText("预算: ",0,20,430,50,100);
			var billText:TextField = Vision.createText("账单: ",0,150,404,50,100);
			var buyText:TextField = Vision.createText("购买: ",0,150,430,50,100);
			infoLayer.addChild(scheduleText);
			infoLayer.addChild(budgetText);
			infoLayer.addChild(billText);
			infoLayer.addChild(buyText);
			infoLayer.addChild(partDes);
			infoLayer.addChild(partName);
			infoLayer.addChild(schedule);
			infoLayer.addChild(budget);
			infoLayer.addChild(bill);
			infoLayer.addChild(buy);
			var introduceText:Image = CustomSkin.getImage("IntroduceText");
			infoLayer.addChild(introduceText);
			introduceText.x = 25;
			introduceText.y = 165;
		}
		
		public function showInfo(pvo:PartVo):void{
			partName.text = pvo.cName;
			partDes.text = pvo.des;
			schedule.text = budget.text = bill.text = buy.text = " ￥ " + pvo.price;
			TweenLite.to(infoLayer,.2,{x:AwayEngine.senceWidth - 276});
//			TweenLite.to(infoLayer,.2,{x:AwayEngine.senceWidth - infoLayer.width});
		}
		
		
	}
}